package com.zq.qingzhu.controller.common;

import com.google.code.kaptcha.impl.DefaultKaptcha;
import com.google.code.kaptcha.util.Config;
import com.zq.qingzhu.common.Constants;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;

import javax.imageio.ImageIO;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;
import java.util.Properties;

@Controller
public class CommonController {

	@Autowired
	private DefaultKaptcha captchaProducer;

	@GetMapping("/common/kaptcha")
	public void defaultKaptcha(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) throws Exception {
		byte[] captchaOutputStream = null;
		ByteArrayOutputStream imgOutputStream = new ByteArrayOutputStream();
		try {
			//生产验证码字符串并保存到session中
			String verifyCode = captchaProducer.createText();
			httpServletRequest.getSession().setAttribute("verifyCode", verifyCode);
			BufferedImage challenge = captchaProducer.createImage(verifyCode);
			ImageIO.write(challenge, "jpg", imgOutputStream);
		} catch (IllegalArgumentException e) {
			httpServletResponse.sendError(HttpServletResponse.SC_NOT_FOUND);
			return;
		}
		captchaOutputStream = imgOutputStream.toByteArray();
		httpServletResponse.setHeader("Cache-Control", "no-store");
		httpServletResponse.setHeader("Pragma", "no-cache");
		httpServletResponse.setDateHeader("Expires", 0);
		httpServletResponse.setContentType("image/jpeg");
		ServletOutputStream responseOutputStream = httpServletResponse.getOutputStream();
		responseOutputStream.write(captchaOutputStream);
		responseOutputStream.flush();
		responseOutputStream.close();
	}

	@GetMapping("/common/mall/kaptcha")
	public void mallKaptcha(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) throws Exception {
		DefaultKaptcha newBeeMallLoginKaptcha = new DefaultKaptcha();
		Properties properties = new Properties();
		properties.put("kaptcha.border", "no");
		properties.put("kaptcha.textproducer.font.color", "27,174,171");
		properties.put("kaptcha.noise.color", "20,33,42");
		properties.put("kaptcha.textproducer.font.size", "30");
		properties.put("kaptcha.image.width", "110");
		properties.put("kaptcha.image.height", "40");
		properties.put("kaptcha.session.key", Constants.MALL_VERIFY_CODE_KEY);
		properties.put("kaptcha.textproducer.char.space", "2");
		properties.put("kaptcha.textproducer.char.length", "6");
		Config config = new Config(properties);
		newBeeMallLoginKaptcha.setConfig(config);
		byte[] captchaOutputStream = null;
		ByteArrayOutputStream imgOutputStream = new ByteArrayOutputStream();
		try {
			//生产验证码字符串并保存到session中
			String verifyCode = newBeeMallLoginKaptcha.createText();
			httpServletRequest.getSession().setAttribute(Constants.MALL_VERIFY_CODE_KEY, verifyCode);
			BufferedImage challenge = newBeeMallLoginKaptcha.createImage(verifyCode);
			ImageIO.write(challenge, "jpg", imgOutputStream);
		} catch (IllegalArgumentException e) {
			httpServletResponse.sendError(HttpServletResponse.SC_NOT_FOUND);
			return;
		}
		captchaOutputStream = imgOutputStream.toByteArray();
		httpServletResponse.setHeader("Cache-Control", "no-store");
		httpServletResponse.setHeader("Pragma", "no-cache");
		httpServletResponse.setDateHeader("Expires", 0);
		httpServletResponse.setContentType("image/jpeg");
		ServletOutputStream responseOutputStream = httpServletResponse.getOutputStream();
		responseOutputStream.write(captchaOutputStream);
		responseOutputStream.flush();
		responseOutputStream.close();
	}
}
